; mwo-auto-launch
; Version 0.1
;
; by Markus "Tailgunner" Holzer
;
; This script launches the Mechwarrior Online Client
; and eliminates the tedious need to enter your password every time
; to play.
;
; To make it work you will need to edit this script or supply your password
; via a command line argument.
;
; This script does not transmit your password anywhere
; except the game server.
;
; This script must be run as administrator because the program it has to control "remotely"
; needs administrator priviliges itself.
;
; See you on the battlefield Mechwarrior


; This is the password the script uses to login to the MWO-Servers
; You MUST change this ( after the = ) or call the script with an argument and specify the password there

MWOPassword = mypassword

; This is the directory where the .exe for Mechwarrior is located
; Change the value after the = if your its location is different on your machine

MWODirectory = C:\Program Files\MechWarrior Online\Bin32





;;;;;;;;;;;; DO NOT CHANGE ANYTHING BEYOND THIS LINE ;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;; UNLESS YOU KNOW WHAT YOU DO ;;;;;;;;;;;;;;;;;; 





; Check if we got command line arguments and set password and working dir accordingly
if %1%
	MWOPassword = %1%

if %2%
	MWODirectory = %2%

; Check for administrator priviliges

if not A_IsAdmin
{
	MsgBox, 48, MWO Auto Launch - Error, This script must be run as administrator.
	ExitApp, -1
}


; Start The MWO Client
StartMWO()
{
	global MWODirectory

	; Indicate Error when the Patcher is not found
	ifExist, %MWODirectory%\MechWarriorOnline.exe
	{
		Run, MechWarriorOnline.exe, %MWODirectory%

		; Wait for the Patcher to launch
		WinWait, MechWarrior Online Patcher,, 20
		
		if ErrorLevel
			return 0
			
		return 1
	}
	
	return 0
}

; Determine if there is a Patch or not
WeHaveAPatch()
{
	WinGetText, Text

	if ( Instr( Text, "Complete!") )
		return 0
	else
		return 1
}

; Function to click the "Patch" Button and wait until the patch process is finished
StartPatching()
{
	; Click "Play"
	Click  750, 550

	while WeHaveAPatch()
		Sleep, 2000
	
	return 1
}

; Function to click the "Play" Button, skip the start screens and enter the password
StartPlaying()
{
	global MWOPassword 

	; Click "Play"
	Click  750, 550
	
	; Wait for Window to load
	; last argument is to Exclude the patcher window since "WinWait" does not look for exact matches 
	WinWait, MechWarrior Online,, 10, MechWarrior Online Patcher 
	Sleep, 4000
	
	; Press Space four times to skip the loading screen
	SendInput {Space}
	Sleep, 300
	SendInput {Space}
	Sleep, 300
	SendInput {Space}
	Sleep, 300
	SendInput {Space}
    Sleep, 300
	SendInput {BackSpace}
	
	; Type the password
	Sleep, 300
	SendInput %MWOPassword%
	
	; Click login
	Click  780, 620
}

if ( StartMWO() )
{
	if ( WeHaveAPatch() )
		StartPatching()

	StartPlaying()
}
else
{
	MsgBox, 48, MWO Auto Launch - Error, Could not start or find the patcher. Make sure the MWO directory is set correctly.
	ExitApp, -2
}

; Exit indicating success
Exit, 0